//
//  VipInfoTabViewController.m
//  fitnessmanage
//
//  Created by 众拳科技 on 2017/10/27.
//  Copyright © 2017年 zongquankeji. All rights reserved.
//

extern NSString * const YZUpdateMenuTitleNote;

#import "VipInfoTabViewController.h"
#import "VipInfoTableViewCell.h"

@interface VipInfoTabViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, assign) NSInteger selectedCol;

@property (nonatomic,strong)UITableView *tableView;

@property (nonatomic,weak) UIView *customView;

@property (nonatomic,assign)CGFloat contentViewHeight;

@end

@implementation VipInfoTabViewController

-(id)init{
    
    self = [super init];
    
    if (self) {
        
        self.isTableShow = YES;
    }
    
    return self;
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    _selectedCol = 0;
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    tableView.dataSource = self;
    tableView.delegate = self;
    tableView.showsVerticalScrollIndicator = NO;
    [tableView registerClass:[VipInfoTableViewCell class] forCellReuseIdentifier:@"VipInfoTableViewCell"];
    tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    tableView.backgroundColor = BackGroundColor;
    [self.view addSubview:tableView];
    self.tableView = tableView;
    [tableView mas_remakeConstraints:^(MASConstraintMaker *make) {

        make.edges.mas_equalTo(self.view);
    }];
    
    UIView *customView = [[UIView alloc] init];
    customView.userInteractionEnabled = YES;
    UITapGestureRecognizer *whiteViewTap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(whiteViewTap)];
    customView.backgroundColor = CSWhiteColor;
    [customView addGestureRecognizer:whiteViewTap];
    [self.view addSubview:customView];
    self.customView = customView;

    UILabel *timeLabel = [[UILabel alloc] init];
    timeLabel.font = MidFont;
    timeLabel.text = @"时间段";
    [customView addSubview:timeLabel];
    UITextField *tf1 = [[UITextField alloc] init];
    tf1.backgroundColor = BackGroundColor;
    tf1.placeholder = @"开始时间";
    tf1.font = SmallFont;
    tf1.textAlignment = NSTextAlignmentCenter;
    [customView addSubview:tf1];

    UIView *verLine = [[UIView alloc] init];
    verLine.backgroundColor = CSBlackColor;
    [customView addSubview:verLine];
    
    UITextField *tf2 = [[UITextField alloc] init];
    tf2.backgroundColor = BackGroundColor;
    tf2.placeholder = @"结束时间";
    tf2.font = SmallFont;
    tf2.textAlignment = NSTextAlignmentCenter;
    [customView addSubview:tf2];
    
    UIButton *backBtn = [[UIButton alloc] init];
    [backBtn addTarget:self action:@selector(backBtnClick)];
    [backBtn setTitleColor:CSBlackColor];
    [backBtn setTitle:@"返回"];
    backBtn.titleLabel.font = MidFont;
    [customView addSubview:backBtn];
    
    UIButton *sureBtn = [[UIButton alloc] init];
    [sureBtn setTitle:@"确定"];
    sureBtn.backgroundColor = TabBarTextColor;
    sureBtn.titleLabel.font = MidFont;
    [customView addSubview:sureBtn];
    
    [customView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.width.mas_equalTo(self.view);
        make.height.mas_equalTo(100);
        make.left.mas_equalTo(self.view.mas_right);
    }];
    
    [timeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(customView).offset(10);
        make.top.mas_equalTo(customView).offset(30);
    }];
    
    [tf2 mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(customView).offset(-10);
        make.centerY.mas_equalTo(timeLabel);
        make.width.mas_equalTo(120);
        make.height.mas_equalTo(30);
    }];
    
    [verLine mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(timeLabel);
        make.right.mas_equalTo(tf2.mas_left).offset(-10);
        make.height.mas_equalTo(1);
        make.width.mas_equalTo(20);
    }];
    
    [tf1 mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(tf2);
        make.right.mas_equalTo(verLine.mas_left).offset(-10);
        make.width.mas_equalTo(120);
        make.height.mas_equalTo(30);
    }];
    
    [backBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.mas_equalTo(customView);
        make.height.mas_equalTo(30);
    }];
    [sureBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(customView);
        make.width.height.mas_equalTo(backBtn);
        make.left.mas_equalTo(backBtn.mas_right);
        make.right.mas_equalTo(customView);
    }];
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:_selectedCol inSection:0];
    
    [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
    
    //说明要更新高度
    NSLog(@"h = %f" ,self.customView.left);
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.titleArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    VipInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"VipInfoTableViewCell"];
    cell.textLabel.text = _titleArray[indexPath.row];
    cell.textLabel.font = MidFont;
    if (indexPath.row == 0) {
        [cell setSelected:YES animated:NO];
    }
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    _selectedCol = indexPath.row;
    
    // 选中当前
    VipInfoTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    if ([cell.textLabel.text isEqualToString:@"自定义"]) {
        
        [UIView animateWithDuration:0.25 animations:^{
            
            [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
                
                make.top.width.height.mas_equalTo(self.view);
                
                make.right.mas_equalTo(self.view.mas_left);
            }];
            
            [self.customView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.width.mas_equalTo(self.view);
                make.height.mas_equalTo(100);
                make.left.mas_equalTo(self.view);
            }];
        }];
    
        self.isTableShow = NO;
        
        // 更新高度
        [[NSNotificationCenter defaultCenter] postNotificationName:@"YZUpdateMenuHeight" object:nil userInfo:@{@"height":@(100),@"col":@(self.colCount)}];
        
        return;
    }
    
    
    // 更新菜单标题
    [[NSNotificationCenter defaultCenter] postNotificationName:YZUpdateMenuTitleNote object:self userInfo:@{@"title":cell.textLabel.text,@"col":@(self.colCount)}];
    
}

-(void)whiteViewTap{
    
    
}

#pragma mark 返回按钮点击
-(void)backBtnClick{
    
    [UIView animateWithDuration:0.25 animations:^{
        
        [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.edges.mas_equalTo(self.view);
        }];
        
        [self.customView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.width.mas_equalTo(self.view);
            make.height.mas_equalTo(100);
            make.left.mas_equalTo(self.view.mas_right);
        }];
    }];
    
    self.isTableShow = YES;
    
    // 更新菜单标题
    [[NSNotificationCenter defaultCenter] postNotificationName:@"YZUpdateMenuHeight" object:nil userInfo:@{@"height":@(210),@"col":@(self.colCount)}];
}
@end
